package com.superyee.commonlib.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.superyee.commonlib.constants.StaticConstant;
import com.superyee.commonlib.utils.gson.GsonHelper;


public class SpUtils {


    private SpUtils(Context context) {
    }


    public static void clear(Context context) {
        SharedPreferences.Editor edit = getEditor(context);
        edit.clear();
        edit.commit();
    }


    public static void remove(Context context, String key) {
        SharedPreferences.Editor edit = getEditor(context);
        edit.remove(key);
        edit.commit();
    }


    public static void putObject(Context context, String key, Object object) {
        Gson gson = new Gson();
        String json = gson.toJson(object);
        putString(context, key, json);
    }

    public static <T> T getObject(Context context, String key, Class<T> clazz) {
        String json = getString(context, key, null);
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        return GsonHelper.fromJson(json, clazz);
    }


    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences.Editor edit = getEditor(context);
        edit.putBoolean(key, value);
        edit.commit();
    }

    public static SharedPreferences.Editor getEditor(Context context) {
        return getSharedPreferences(context).edit();
    }

    public static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(StaticConstant.SP.SPREF_NAME, Context.MODE_PRIVATE);
    }

    public static void putString(Context context, String key, String value) {
        getEditor(context).putString(key, value).commit();
    }

    public static String getString(Context context, String key, String defaultValue) {
        return getSharedPreferences(context).getString(key, defaultValue);
    }

    public static void putLong(Context context, String key, long value) {
        SharedPreferences.Editor edit = getEditor(context);
        edit.putLong(key, value);
        edit.commit();
    }

    public static void putInt(Context context, String key, int value) {
        SharedPreferences.Editor edit = getEditor(context);
        edit.putInt(key, value);
        edit.commit();
    }

    public static int getInt(Context context, String key, int defaultValue) {
        try {
            return getSharedPreferences(context).getInt(key, defaultValue);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static long getLong(Context context, String key, long defaultValue) {
        try {
            return getSharedPreferences(context).getLong(key, defaultValue);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        try {
            return getSharedPreferences(context).getBoolean(key, defaultValue);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static boolean containsKey(Context context, String key) {
        return getSharedPreferences(context).contains(key);
    }


}